#include "bsp_dma.h"

/**
 * @brief  DMA初始化，配置DMA模式、中断使能等
 * @param  dma_handle 串口句柄
 * @param  len        设置DMA缓冲的最大字节数
 * @author bagy
 * @date   2024/12/19
 */
void bsp_dma_init(bsp_dma_t *dma_handle, uint32_t len)
{
    rcu_periph_clock_enable(dma_handle->rcu_clk);

    dma_deinit(dma_handle->dma_periph, dma_handle->channel); 

    dma_handle->dma_init.memory0_addr = (uint32_t)dma_handle->tran_buff;
    dma_handle->dma_init.number       = len;
    dma_single_data_mode_init(dma_handle->dma_periph, dma_handle->channel, &dma_handle->dma_init);

    /* configure DMA mode */
    dma_circulation_disable(dma_handle->dma_periph, dma_handle->channel);
    dma_channel_subperipheral_select(dma_handle->dma_periph, dma_handle->channel, dma_handle->sub_periph);

    dma_interrupt_flag_clear(dma_handle->dma_periph, dma_handle->channel, DMA_FLAG_HTF);
    dma_interrupt_flag_clear(dma_handle->dma_periph, dma_handle->channel, DMA_FLAG_FTF);

    /* enable transfer complete interrupt */
    dma_interrupt_enable(dma_handle->dma_periph, dma_handle->channel, dma_handle->tran_int);
    
    dma_channel_disable(dma_handle->dma_periph, dma_handle->channel);
}

/**
 * @brief  使能DMA通道
 * @param  dma_handle DMA句柄
 * @author bagy
 * @date   2024/12/24
 */
void bsp_dma_enable(bsp_dma_t *dma_handle)
{
    dma_channel_enable(dma_handle->dma_periph, dma_handle->channel);
}

/**
 * @brief  失能DMA通道
 * @param  dma_handle DMA句柄
 * @author bagy
 * @date   2024/12/24
 */
void bsp_dma_disable(bsp_dma_t *dma_handle)
{
    dma_channel_disable(dma_handle->dma_periph, dma_handle->channel);
}
